<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('adszoneZone.quick Search Fields') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

const { t } = useI18n()
const tableRef = ref()
interface Props {
    zoneId?: string
}
const props = withDefaults(defineProps<Props>(), {
    zoneId: '',
})
const state = reactive({
    defalutZoneId: props.zoneId,
})

const optButtons = defaultOptButtons(['weigh-sort', 'edit', 'delete'])
const baTable = new baTableClass(
    new baTableApi('/admin/AdszoneAds/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('adszoneZone.zone_id'), prop: 'zone_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: '=', show: false },
            {
                label: t('adszoneZone.adszone_zone__name'),
                prop: 'adszoneZone.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
                operator: false,
                replaceValue: {},
            },
            {
                label: t('adszoneZone.title'),
                prop: 'title',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('adszoneZone.image'),
                prop: 'image',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'image',
                operator: false,
            },
            {
                label: t('adszoneZone.effectime'),
                prop: 'effectime',
                align: 'center',
                render: 'tag',
                operator: false,
                width: 160,

            },
            {
                label: t('adszoneZone.expiretime'),
                prop: 'expiretime',
                align: 'center',
                render: 'tag',
                operator: false,
                width: 160,
            },
            {
                label: t('adszoneZone.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 140, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined],
        defaultOrder: { prop: 'weigh', order: 'desc' },
    },
    {
        defaultItems: { target: '_blank', zone_id: state.defalutZoneId },
    }
)
state.defalutZoneId ? (baTable.table.filter!.search = [{ field: 'zone_id', val: state.defalutZoneId, operator: '=' }]) : ''
provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

defineOptions({
    name: 'adszoneAds',
})
</script>

<style scoped lang="scss"></style>
